/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: Frank
 * 系统版本: 1.0.0
 * 开发人员: Frank
 * 开发时间: 2019/10/28 18:47
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.web.converter;

import com.spring.arch.uaa.web.dto.WebDepartmentDTO;
import com.spring.arch.uaa.web.dto.WebUserSimpleDTO;
import com.spring.arch.uaa.model.Organization;
import com.spring.arch.uaa.model.OrganizationEmployee;
import com.spring.arch.uaa.model.User;
import com.spring.arch.uaa.repository.OrganizationEmployeeRepository;
import com.spring.arch.uaa.repository.OrganizationRepository;
import com.spring.arch.uaa.web.dto.WebDepartmentDTO;
import com.spring.arch.uaa.web.dto.WebUserSimpleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 部门转换器
 *
 * @author Frank
 * @version 1.0.0.1
 * @since JDK 1.8
 */
@Component
public class WebDepartmentConverter {

    @Autowired
    private OrganizationRepository organizationRepository;

    @Autowired
    private OrganizationEmployeeRepository organizationEmployeeRepository;

    public WebDepartmentDTO toTreeDTO(Organization organization) {

        WebDepartmentDTO dto = new WebDepartmentDTO();

        dto.setId(organization.getId());
        dto.setName(organization.getName());
        dto.setChildren(toTree(organization));

        dto.setParentId(organization.getParent().getId());
        dto.setUsers(toUserSimpleDTOList(organizationEmployeeRepository.findByOrganization(organization).stream()
                .map(OrganizationEmployee:: getUser).collect(Collectors.toList())));

        return dto;
    }

    private List<WebDepartmentDTO> toTree(Organization organization) {

        List<WebDepartmentDTO> childrenDto = new ArrayList<>();

        List<Organization> list =
                organizationRepository.findAllByParentIdAndDisableFalseOrderByCreatedDate(organization.getId());

        if(!CollectionUtils.isEmpty(list)) {
            list.forEach(l -> {

                WebDepartmentDTO dto = new WebDepartmentDTO();
                dto.setId(l.getId());
                dto.setParentId(organization.getId());
                dto.setName(l.getName());
                dto.setChildren(toTree(l));
                dto.setUsers(toUserSimpleDTOList(organizationEmployeeRepository.findByOrganization(l).stream()
                        .map(OrganizationEmployee::getUser).collect(Collectors.toList())));

                childrenDto.add(dto);
            });
        }

        return childrenDto;
    }

    private List<WebUserSimpleDTO> toUserSimpleDTOList(List<User> users) {
        return users.stream().filter(Objects::nonNull)
                .map(user -> new WebUserSimpleDTO(user.getId(), user.getAccount(), user.getRealName()))
                .collect(Collectors.toList());
    }
}
